// ----------------------------------
// RSDK Project: Sonic Essence
// Script Description: Soul Octopoid Body
// Script Author: Tails LP
// ----------------------------------

//-------Aliases-------//

//States

private alias 0 : ARM_IDLE
private alias 1 : ARM_MOVEX
private alias 2 : ARM_SLAMONGROUND
private alias 3 : ARM_EXPLODE


private alias object.value0 : arm.timer
private alias object.value1 : arm.xvel
private alias object.value2 : arm.animation
private alias object.value3 : arm.aniTimer

private alias object.value4 : check.overlap0
private alias object.value5 : check.overlap1
private alias object.value6 : check.overlap2
private alias object.value7 : check.overlap3

private alias object.value8 : arm.health
private alias object.value9 : arm.cooldown

private alias object.value10 : boss.screenShake

private alias object.value11 : arm.explodeTimer


private alias object.value38 : player.hitboxLeft
private alias object.value39 : player.hitboxRight
private alias object.value40 : player.hitboxTop
private alias object.value41 : player.hitboxBottom

//-------Function declarations-------//
reserve function Arm_Hit

//-------Static Values-------//

public value arm.size = 0;

//-------Tables-------//


//-------Functions-------//

function Arm_Hit
	arm.health--
	if arm.health == 0
		player.score += 1000
		object.state = ARM_EXPLODE
		arm.timer = 0
	else
		boss.health.max -= 10
		arm.cooldown = 32
		PlaySfx(SfxName[Octo Hit Itself], false)
	end if
end function

event ObjectMain
	object.priority = PRIORITY_ACTIVE
	if arm.animation == 4
		object.drawOrder = 4
	else
		object.drawOrder = 3
	endif

	switch object.state
	case ARM_IDLE

		object.xvel = 0x20000


		arm.animation = 0
		
		break
		
	case ARM_MOVEX
		arm.animation = 0
		if object.direction == 1
			object.xpos -= object.xvel
		else
			object.xpos += object.xvel
		endif
		
		switch object.propertyValue
		case 0
			foreach (TypeName[Soul Octopoid Arms], arrayPos1, ALL_ENTITIES)
				if object.propertyValue == 0
					if object.propertyValue != object[arrayPos1].propertyValue
						BoxCollisionTest(C_TOUCH, object.entityPos, -10, -10, 10, -9, arrayPos1, -10, -10, 10, -9)
					endif
					check.overlap0 = checkResult
				endif
			next
			break
		case 1
			foreach (TypeName[Soul Octopoid Arms], arrayPos2, ALL_ENTITIES)
				if object.propertyValue == 1
					if object.propertyValue != object[arrayPos2].propertyValue
						BoxCollisionTest(C_TOUCH, object.entityPos, -10, -8, 10, -7, arrayPos2, -10, -8, 10, -7)
					endif
					check.overlap1 = checkResult
				endif
			next
			break
		case 2
			foreach (TypeName[Soul Octopoid Arms], arrayPos3, ALL_ENTITIES)
				if object.propertyValue == 2
					if object.propertyValue != object[arrayPos3].propertyValue
						BoxCollisionTest(C_TOUCH, object.entityPos, -10, -6, 10, -5, arrayPos3, -10, -6, 10, -5)
					endif
					check.overlap2 = checkResult
				endif
			next
			break
		case 3
			foreach (TypeName[Soul Octopoid Arms], arrayPos4, ALL_ENTITIES)
				if object.propertyValue == 3
					if object.propertyValue != object[arrayPos4].propertyValue
						BoxCollisionTest(C_TOUCH, object.entityPos, -10, -4, 10, -3, arrayPos4, -10, -4, 10, -3)
					endif
					check.overlap3 = checkResult
				endif
			next
			break
		endswitch
		
		break
		
	case ARM_SLAMONGROUND
		arm.aniTimer ++
		switch arm.aniTimer
		case 20
			arm.animation = 0
			break
		case 25
			arm.animation = 1
			break
		case 30
			arm.animation = 2
			break
		case 35
			arm.animation = 3
			break
		case 40
			arm.animation = 4
			PlaySfx(SfxName[Octo Arm Press], false)
			break
		case 210
			arm.animation = 3
			break
		case 215
			arm.animation = 2
			break
		case 220
			arm.animation = 1
			break
		case 225
			arm.animation = 0
			break
		case 245
			arm.aniTimer = 0
			object.state = ARM_IDLE
			break
		endswitch
		
		if arm.animation == 4
			if arm.health != 0
				if arm.cooldown > 0
					arm.cooldown--
				end if
				foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
					BoxCollisionTest(C_TOUCH, object.entityPos, -8, 32, 8, 48, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
					if checkResult == TRUE
						CallFunction(PlayerObject_CheckHit)
						if checkResult == TRUE
							callFunction(Arm_Hit)
						endif
					endif
				next
			endif
		endif
		break
	case ARM_EXPLODE
		arm.timer ++
		if arm.timer >= 30
			object.state ++
		endif
		arm.explodeTimer++
		temp0 = arm.explodeTimer
		temp0 &= 7
		if temp0 == 4
			Rand(temp0, 48)
			temp0 -= 48
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			PlaySfx(SfxName[Octo Arm Explode], false)
		end if
		break
	endswitch
	

end event

event ObjectDraw

		temp2 = stage.curYBoundary2
		temp2 -= object.ixpos
		temp2 /= 32
		
		temp0 = 5
		temp1 = object.ypos
		temp3 = object.iypos
		while temp3 <= stage.curYBoundary2		// draws the arms till the bottom of the screen
			if temp3 == object.iypos
				// nothing (tip draw code was here, moved to bottom because it needs to be drawn on top)
			else
				if temp0 == 0
					temp0 += arm.animation						// for the slam animation
					DrawSpriteXY(temp0, object.xpos, temp1) 	// draws the red secments 
					temp0 -= arm.animation						// remove this again to return to the base number
				else
					DrawSpriteXY(temp0, object.xpos, temp1) 	// draws the blue secments (don't need slam ani, since not visible duing the attack)
				endif
			endif
			temp1 += 0x200000
			temp0 ^= 5
			temp3 += 32
		loop
		
		// Draw the tip
		
		temp1 = object.ypos
		temp4 = arm.animation
		temp4 += 10
		
		temp5 = arm.animation
		temp5 *= 8
		temp5 *= 65536
		temp1 += temp5								// for the slam animation
		DrawSpriteXY(temp4, object.xpos, temp1)		// draws the tip
		temp1 -= temp5								// remove this again to return to the base number
end event

event ObjectStartup
    LoadSpriteSheet("SSZ/Objects2.gif")
	
	SpriteFrame(-10, -16, 20, 32, 160, 1)	
	SpriteFrame(-10, -16, 20, 32, 181, 1)	
	SpriteFrame(-10, -16, 20, 32, 202, 1)	
	SpriteFrame(-10, -16, 20, 32, 223, 1)	
	SpriteFrame(-10, -16, 20, 32, 244, 1)	
	
	SpriteFrame(-10, -16, 20, 32, 160, 34)	
	SpriteFrame(-10, -16, 20, 32, 181, 34)	
	SpriteFrame(-10, -16, 20, 32, 202, 34)	
	SpriteFrame(-10, -16, 20, 32, 223, 34)	
	SpriteFrame(-10, -16, 20, 32, 244, 34)
	
	SpriteFrame(-10, -16, 20, 32, 160, 67)	
	SpriteFrame(-10, -16, 20, 32, 181, 67)	
	SpriteFrame(-10, -16, 20, 32, 202, 67)	
	SpriteFrame(-10, -16, 20, 32, 223, 67)	
	SpriteFrame(-10, -16, 20, 32, 244, 67)
	
	
	foreach (TypeName[Soul Octopoid Body], arrayPos0, ALL_ENTITIES)
		rise[arrayPos0].topYPos = object[arrayPos0].ypos
	next
	
	foreach (TypeName[Soul Octopoid Arms], arrayPos1, ALL_ENTITIES)
		object[arrayPos1].value8 = 3
	next
		
		
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 223, 172)
end event